package com.learn2.config;

import com.learn2.other.DemoInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/**
 * @Author:Mark
 * @Description:
 * @Date: Created on 16:58 2018-11-13
 * @Modify by:
 */
@Configuration
@EnableWebMvc
//@ComponentScan("com.learn2.*")
public class WebMvcConfig extends WebMvcConfigurerAdapter {

  @Bean
  public InternalResourceViewResolver viewResolver(){

    InternalResourceViewResolver viewResolver=new InternalResourceViewResolver();
    viewResolver.setPrefix("WEB-INF/classes/views/");
    viewResolver.setSuffix(".jsp");
    viewResolver.setViewClass(JstlView.class);
    return viewResolver;
  }

  //产生一个DemoInterceptor
  @Bean
  public DemoInterceptor getDemoInterceptor(){
    return new DemoInterceptor();
  }

  //注册这个拦截器
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(getDemoInterceptor());
  }

}
